package com.yuanlrc.hotel.RoomReserve.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.yuanlrc.hotel.room.domain.Room;
import com.yuanlrc.hotel.room.mapper.RoomMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yuanlrc.hotel.RoomReserve.mapper.RoomReserveMapper;
import com.yuanlrc.hotel.RoomReserve.domain.RoomReserve;
import com.yuanlrc.hotel.RoomReserve.service.IRoomReserveService;
import com.yuanlrc.common.core.text.Convert;

/**
 * 预约管理Service业务层处理
 * 
 * @author yuanlrc
 * @date 2020-11-19
 */
@Service
public class RoomReserveServiceImpl implements IRoomReserveService 
{

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private RoomReserveMapper roomReserveMapper;

    /**
     * 查询预约管理
     * 
     * @param id 预约管理ID
     * @return 预约管理
     */
    @Override
    public RoomReserve selectRoomReserveById(Long id)
    {
        return roomReserveMapper.selectRoomReserveById(id);
    }


    /**
     * 对当前用户的预约记录进行操作处理
     * @param id
     * @return
     */
    @Override
    public List<RoomReserve> selectAllRoomReserve(Long id) {
        List<RoomReserve> roomReserves = roomReserveMapper.selectRoomReserveByAccountId(id);
        for (RoomReserve roomReserve : roomReserves) {
            //获得房间信息
            Room room = roomMapper.selectRoomById(roomReserve.getRoomId().intValue());
            roomReserve.setRoom(room);
            //预订的开始时间
            Date start = roomReserve.getStartDateTime();
            //预订的酒店时间
            Date end = roomReserve.getEndDateTime();
            //预订的时长（天/时）
            Integer count = 0;
            //判断是普通房还是钟点房
            if (room.getIsTimeLimit() == Room.NOT_TimeLimit){
                while(start.compareTo(end) < 0){
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(start);
                    cal.add(Calendar.DAY_OF_MONTH, 1);
                    start = cal.getTime();
                    count ++;
                }
                roomReserve.setPrices(count * room.getPrice());
            }else if(room.getIsTimeLimit() == Room.TimeLimit) {
                while(start.compareTo(end) < 0){
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(start);
                    cal.add(Calendar.HOUR_OF_DAY, 1);
                    start = cal.getTime();
                    count ++;
                }
                roomReserve.setPrices(count * room.getPrice());
            }

            roomReserve.setCount(count);

            if(new Date().compareTo(roomReserve.getStartDateTime()) > 0 )
            {
                roomReserve.setStatus(RoomReserve.NOT_REMOVE);
            }
            else
            {
                roomReserve.setStatus(RoomReserve.REMOVE);
            }
        }

        return roomReserves;
    }

    /**
     * 查询当前用户的预约记录
     * @param accountId
     * @return
     */
    @Override
    public List<RoomReserve> selectRoomReserveByAccountId(Long accountId) {
        return roomReserveMapper.selectRoomReserveByAccountId(accountId);
    }

    /**
     * 查询预约管理列表
     * 
     * @param roomReserve 预约管理
     * @return 预约管理
     */
    @Override
    public List<RoomReserve> selectRoomReserveList(RoomReserve roomReserve)
    {
        return roomReserveMapper.selectRoomReserveList(roomReserve);
    }

    /**
     * 新增预约管理
     * 
     * @param roomReserve 预约管理
     * @return 结果
     */
    @Override
    public int insertRoomReserve(RoomReserve roomReserve)
    {
        return roomReserveMapper.insertRoomReserve(roomReserve);
    }

    /**
     * 修改预约管理
     * 
     * @param roomReserve 预约管理
     * @return 结果
     */
    @Override
    public int updateRoomReserve(RoomReserve roomReserve)
    {
        return roomReserveMapper.updateRoomReserve(roomReserve);
    }

    /**
     * 删除预约管理对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteRoomReserveByIds(String ids)
    {
        return roomReserveMapper.deleteRoomReserveByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除预约管理信息
     * 
     * @param id 预约管理ID
     * @return 结果
     */
    @Override
    public int deleteRoomReserveById(Long id)
    {
        return roomReserveMapper.deleteRoomReserveById(id);
    }

    /**
     * true在别人的预定时间内
     * @param startDateTime
     * @return
     */
    @Override
    public boolean isMakeStartDateTime(String startDateTime) {
        return roomReserveMapper.isMakeStartDateTime(startDateTime) > 0;
    }

    @Override
    public List<RoomReserve> selectDate(Integer accountId, Integer roomId, String datetime) {

        return roomReserveMapper.selectDate(accountId, roomId, datetime);
    }

    @Override
    public List<RoomReserve> selectRoomReserveListByRoomId(String start, Integer roomId) {
        return roomReserveMapper.selectRoomReserveListByRoomId(start, roomId);
    }

    @Override
    public int selectRoomReserveCount(Integer roomId, String start, String end) {
        return roomReserveMapper.selectRoomReserveCount(roomId, start, end);
    }

    @Override
    public RoomReserve selectStartSection(Integer roomId, String start) {
        return roomReserveMapper.selectStartSection(roomId, start);
    }

    @Override
    public RoomReserve selectEqualStartDate(Integer roomId, String start) {
        return roomReserveMapper.selectEqualStartDate(roomId, start);
    }
}
